/*
Morse Code Conversion(Unicode)
Copyright (C) 2020 sosei

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
      var MCarr=new Array()  //下一版本改Map对象
      MCarr[0]=["a",".-"]
      MCarr[1]=["b","-..."]
      MCarr[2]=["c","-.-."]
      MCarr[3]=["d","-.."]
      MCarr[4]=["e","."]
      MCarr[5]=["è","..-.."]
      MCarr[6]=["f","..-."]
      MCarr[7]=["g","--."]
      MCarr[8]=["h","...."]
      MCarr[9]=["i",".."]
      MCarr[10]=["j",".---"]
      MCarr[11]=["k","-.-"]
      MCarr[12]=["l",".-.."]
      MCarr[13]=["m","--"]
      MCarr[14]=["n","-."]
      MCarr[15]=["o","---"]
      MCarr[16]=["p",".--."]
      MCarr[17]=["q","--.-"]
      MCarr[18]=["r",".-."]
      MCarr[19]=["s","..."]
      MCarr[20]=["t","-"]
      MCarr[21]=["u","..-"]
      MCarr[22]=["v","...-"]
      MCarr[23]=["w",".--"]
      MCarr[24]=["x","-..-"]
      MCarr[25]=["y","-.--"]
      MCarr[26]=["z","--.."]
      MCarr[27]=["0","-----"]
      MCarr[28]=["1",".----"]
      MCarr[29]=["2","..---"]
      MCarr[30]=["3","...--"]
      MCarr[31]=["4","....-"]
      MCarr[32]=["5","....."]
      MCarr[33]=["6","-...."]
      MCarr[34]=["7","--..."]
      MCarr[35]=["8","---.."]
      MCarr[36]=["9","----."]
      MCarr[37]=[".",".-.-.-"]
      MCarr[38]=[",","--..--"]
      MCarr[39]=[":","---..."]
      MCarr[40]=["?","..--.."]
      MCarr[41]=["’",".----."]
      MCarr[42]=["-","-....-"]
      MCarr[43]=["/","-..-."]
      MCarr[44]=["(","-.--."]
      MCarr[45]=[")","-.--.-"]
      MCarr[46]=["\"",".-..-."]
      MCarr[47]=["=","-...-"]
      MCarr[48]=["{Understood}","...-."]
      MCarr[49]=["{Error}","........"]
      MCarr[50]=["+",".-.-."]
      MCarr[51]=["{Wait}",".-..."]
      MCarr[52]=["{End of work}","...-.-"]
      MCarr[53]=["{Starting signal}","-.-.-"]
      MCarr[54]=["@",".--.-."]
      MCarr[55]=[" "," "]
      
      function Char_To_Unicode_number(one_character)
        {var a_unicode_number
         a_unicode_number = one_character.charCodeAt(0).toString(16)
         for(var i = a_unicode_number.length; i<4; ++i)
           {a_unicode_number = '0' + a_unicode_number
           }
         return 'U+' + a_unicode_number.toUpperCase()
        }
      
      function Unicode_number_To_Char(a_unicode_number)
        {return String.fromCharCode(parseInt(a_unicode_number.toUpperCase().replace(/U\+/, '0x'), 16))
        }
        
      function Do_MCarr_Match(mcarr_key, match_type_flag)
        {var mess = ''
         for(var i in MCarr)
           {if(match_type_flag == 'char')
              {if(mcarr_key.length == 1)
                 {mcarr_key = mcarr_key.toLowerCase()
                 }
               if(mcarr_key == MCarr[i][0])
                 {mess = MCarr[i][1]
                  break
                 }
              }
            else if (match_type_flag == 'code')
              {if(mcarr_key == MCarr[i][1])
                 {mess = MCarr[i][0]
                  break
                 }
              }
           }
         return mess
        }
      
      function Telegraph_text_To_Morse_code(telegraph_text)
        {var mess = []
         var translated_string
         for (var messarr of telegraph_text.match(/(\{[\w #]+?\}|[\S\s])/mg))
           {translated_string = Do_MCarr_Match(messarr, 'char')
            if (translated_string != '')
              {mess.push(translated_string)
              }
            else
              {if (messarr.length == 1)
                 {mess = mess.concat(Telegraph_text_To_Morse_code(Char_To_Unicode_number(messarr)))
                 }
               else
                 {mess.push('{#}')
                 }
              }
           }
         return mess
        }
      
      function Morse_code_To_Telegraph_text(morse_code_text)
        {var mess = ''
         var messarr
         var translated_string
         var morse_code_decoding_pattern = /([.-]+)|( {3,})|(\{#\})/g
         var space_len
         
         while ((messarr = morse_code_decoding_pattern.exec(morse_code_text)) != null)
           {if (messarr[1] != null)
              {translated_string = Do_MCarr_Match(messarr[1], 'code')
               if (translated_string == '')
                 {translated_string = '{#}'
                 }
               mess += translated_string
              }
            else if (messarr[2] != null)
              {space_len = messarr[2].length
               if (space_len == 3)
                 {mess += ' '
                 }
               else
                 {mess += ' '.repeat(1 + parseInt((space_len - 3) / 2))
                 }
              }
            else if (messarr[3] != null)
              {mess += messarr[3]
              }
           }
         return mess
        }
      
      function Do_Morse_Encrypt(telegraph_text)
        {return Telegraph_text_To_Morse_code(telegraph_text).join(' ')
        }
        
      function Do_Morse_Decrypt(morse_code_text)
        {return Morse_code_To_Telegraph_text(morse_code_text).replace(/U\+([0-9A-F]{4})/ig, Unicode_number_To_Char)
        }